package com.dyz.swordFingerOffer;

/**
 * @author: daiyizheng
 * @date: 2021/5/30 20:13
 * @description:
 */
public class Merge25 {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null & list1==null){
            return null;
        }
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        ListNode root;
        if(list1.val>=list2.val){
            root = new ListNode(list2.val);
            list2 = list2.next;
        }else{
            root = new ListNode(list1.val);
            list1 = list1.next;
        }

        ListNode cur = root;
        while(list1!=null && list2!=null){
            if(list1.val>=list2.val){
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }else{
                cur.next = new ListNode(list1.val);
                list1 = list1.next;
            }
            cur = cur.next;
        }
        while(list1!=null){
            cur.next = new ListNode(list1.val);
            list1 = list1.next;
            cur = cur.next;
        }

        while(list2!=null){
            cur.next = new ListNode(list2.val);
            list2 = list2.next;
            cur = cur.next;
        }
        return root;
    }

    //使用递归的方式
    public ListNode Merge1(ListNode list1,ListNode list2) {
        if(list1 == null) {return list2;}
        if(list2 == null) {return list1;}
        if(list1.val<list2.val){
            list1.next = Merge1(list1.next, list2);
            return list1;
        }else {
            list2.next = Merge1(list1,list2.next);
            return list2;
        }

    }
}
